go to previous page   go to home page   go to next page

Answer:

3


Returned Value used by a Caller

Activations and Returns

Each caller gets a value back from the method it activated. The caller then uses this value in the addition and returns the result. The chain of activation "unwinds" as the values are returned.

int Triangle( int N )
{
  if ( N == 1 )
    return 1;
  else
    return N + Triangle( N-1 );
}

Finally the very first activation gets a 6 back from the method it activated, so it computes a 10 and returns that to its caller.

After an activation has returned a value to its caller, it is no longer active and is removed from the chain of activations. The diagram does not show this.


QUESTION 17:

This example computed Triangle(4). How many activations of the method Triangle() were there?